package gang.org.springframework.framework.factory.config;

import gang.org.springframework.framework.factory.GangBeanFactory;
import gang.org.springframework.framework.factory.GangInjectionPoint;
import gang.org.springframework.framework.support.GangMethodParameter;

import java.io.Serializable;
import java.lang.reflect.Field;

public class GangDependencyDescriptor extends GangInjectionPoint implements Serializable {

    public final Class<?> declaringClass;

    public String methodName;

    public Class<?>[] parameterTypes;

    public int parameterIndex;

    public Class<?> containingClass;

    public final boolean required;

    public final boolean eager;

    public Field field;


    public GangDependencyDescriptor(GangDependencyDescriptor original)
    {
        super(original);
        //TODO
        this.declaringClass = original.declaringClass;
        this.required = original.required;
        this.eager = original.eager;
    }

    public GangDependencyDescriptor(GangMethodParameter methodParameter, boolean required){
        this(methodParameter, required, true);
    }

    public GangDependencyDescriptor(GangMethodParameter methodParameter, boolean required, boolean eager){
        super(methodParameter);
        this.declaringClass = methodParameter.getDeclaringClass(); //声明该构造函数的类或接口
        if (methodParameter.getMethod() !=null) {
            this.methodName = methodParameter.getMethod().getName();
        }
        this.parameterTypes = methodParameter.getExecutable().getParameterTypes();//构造函数声明参数对象
        this.parameterIndex = methodParameter.getParameterIndex();//当前构造参数的index
        this.containingClass = methodParameter.getContainingClass();//声明该构造函数的类或接口
        this.required = required;
        this.eager = eager;
    }

    public Class<?> getDependencyType(){
        if (this.field != null) {
            //TOOD
            return null;
        }else {
            return obtainMethodParameter().getNestedParameterType();
        }
    }

    public Object resolveCandidate(String beanName, Class<?> requiredType, GangBeanFactory beanFactory){
        return beanFactory.getBean(beanName);
    }

}
